package com.zcs.app.lucky.ui

import android.content.Intent
import android.view.View
import cn.sxw.android.base.adapter.CommonRecyclerAdapter
import cn.sxw.android.base.kt.BaseViewModel
import cn.sxw.android.base.utils.JColors
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zcs.app.lucky.R
import com.zcs.app.lucky.base.BaseLuckyActivity
import com.zcs.app.lucky.data.LuckyDataCache
import com.zcs.app.lucky.databinding.ActivityLuckyHistoryBinding
import com.zcs.app.lucky.entity.LuckyEntity
import java.util.*

class LuckyHistoryActivity : BaseLuckyActivity<ActivityLuckyHistoryBinding, BaseViewModel>() {
    private var mAdapter: CommonRecyclerAdapter<LuckyEntity>? = null
    private val mItems = ArrayList<LuckyEntity>()

    // 懒加载机制
    override fun onReady() {
        binding.toolbar.tvToolbarTitle.text = "抽奖记录"
        binding.loadingLayout.visibility = View.VISIBLE
    }

    override fun onInit() {
        initView()
        initObserve()
        initData()
    }

    override fun initObserve() {
    }

    override fun initView() {
    }

    override fun initData() {
        mItems.clear()
        val list = LuckyDataCache.loadAll()
        if (list.isNotEmpty()) {
            mItems.addAll(list)
            binding.tvTips.visibility = View.GONE
            binding.btnCreateNow.visibility = View.GONE
        } else {
            binding.tvTips.visibility = View.VISIBLE
            binding.btnCreateNow.visibility = View.VISIBLE
        }
        initAdapter()
        hideLoading()
    }

    private fun initAdapter() {
        if (mAdapter == null) {
            mAdapter =
                object : CommonRecyclerAdapter<LuckyEntity>(R.layout.item_lucky_row, mItems) {
                    override fun convert(holder: BaseViewHolder, item: LuckyEntity) {
                        holder.setText(R.id.tvName, item.niceName())
                        holder.setText(R.id.tvDesc, item.niceDesc())
                        holder.setText(R.id.tvStatus, item.statusName())
                        holder.setTextColor(
                            R.id.tvStatus,
                            if (item.canLucky()) JColors.green else JColors.red
                        )
                    }
                }
            binding.rvUsers.adapter = mAdapter
            mAdapter!!.setOnItemClickListener { _, _, position ->
                val clickItem = mItems[position]
                onItemClick(clickItem)
            }
        } else {
            mAdapter!!.notifyDataSetChanged()
        }
    }

    // 创建或者修改
    private fun onItemClick(item: LuckyEntity) {
        goAdd = true
        if (item.canLucky()) {// 继续抽奖
            val intent = Intent(this, LuckyResumeActivity::class.java)
            intent.putExtra(KEY_ID, item.id)
            launchActivity(intent)
        } else {// 查看结果
            val intent = Intent(this, LuckyResultActivity::class.java)
            intent.putExtra(KEY_ID, item.id)
            launchActivity(intent)
        }
    }

    override fun hideLoading() {
        super.hideLoading()
        binding.loadingLayout.visibility = View.GONE
    }

    override fun addOnClickListener() {
        binding.toolbar.ivToolbarBack.setOnClickListener { onBackPressed() }
        binding.btnCreateNow.setOnClickListener {
            launchActivity(CreateLuckyActivity::class.java)
            killMyself()
        }
    }
}